﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace BasicCMS
{
    public static class Util
    {
        /// <summary>
        /// 加载应用程序的程序集
        /// </summary>
        /// <returns></returns>
        public static Assembly[] LoadAppAssemblies()
        {
            string binFolder = AppDomain.CurrentDomain.RelativeSearchPath;
            if (String.IsNullOrEmpty(binFolder))
                binFolder = Environment.CurrentDirectory;

            DirectoryInfo binInfo = new DirectoryInfo(binFolder);
            string[] files = binInfo.GetFiles("BasicCMS*.dll").Select(m => m.Name).ToArray();

            return files.Select(m => Assembly.Load(Path.GetFileNameWithoutExtension(m)))
                .ToArray();
        }

        /// <summary>
        /// 将字符串转化为Int值
        /// </summary>
        /// <param name="strVal"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static int ToInt(this string strVal, int defaultValue = 0)
        {
            int result;
            return Int32.TryParse(strVal, out result) ? result : defaultValue;
        }

        /// <summary>
        /// 判定字符串IsNullOrEmpty
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this string source)
        {
            return String.IsNullOrEmpty(source);
        }

        /// <summary>
        /// 判定字符串IsNullOrWhiteSpace
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool IsNullOrWhiteSpace(this string source)
        {
            return string.IsNullOrWhiteSpace(source);
        }

        /// <summary>
        /// 判定两个字符串是否相等
        /// </summary>
        /// <param name="str"></param>
        /// <param name="val"></param>
        /// <param name="ignoreCase"></param>
        /// <returns></returns>
        public static bool IsEqual(this string str, string val, bool ignoreCase = false)
        {
            if (str == null && val == null)
                return true;

            if (str == null || val == null)
                return false;

            return String.Compare(str, val, ignoreCase) == 0;
        }

        /// <summary>
        /// 对枚举器的每个元素执行指定的操作
        /// </summary>
        /// <typeparam name="T">枚举器类型参数</typeparam>
        /// <param name="source">枚举器</param>
        /// <param name="action">要对枚举器的每个元素执行的委托</param>
        public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
        {
            if (source.IsNullOrEmpty() || action == null)
            {
                return;
            }
            foreach (var item in source)
            {
                action(item);
            }
        }

        /// <summary>
        /// 指示指定的枚举器是null还是没有任何元素
        /// </summary>
        /// <typeparam name="T">枚举器类型参数</typeparam>
        /// <param name="source">要测试的枚举器</param>
        /// <returns>true:枚举器是null或者没有任何元素 false:枚举器不为null并且包含至少一个元素</returns>
        public static bool IsNullOrEmpty<T>(this IEnumerable<T> source)
        {
            return source == null || !source.Any();
        }

        /// <summary>
        /// 获取类型的CustomAttribute
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="type"></param>
        /// <param name="inherit"></param>
        /// <returns></returns>
        public static T GetCustomAttribute<T>(this Type type, bool inherit = false) where T : Attribute
        {
            if (type.IsDefined(typeof(T)))
            {
                return (T)type.GetCustomAttributes(typeof(T), inherit)[0];
            }
            return default(T);
        }
    }
}
